﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using OTS.Utility;
using DevExpress.XtraEditors;

namespace OTS
{
    public partial class ManualOdeme : XForm
    {
        public ManualOdeme()
        {
            InitializeComponent();

        }


        private void OdemeTablosu_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            DataTable dt = AppSession.DataManager.ExecuteDataTable("GetManualOdeme");
            dt.TableNewRow += dt_TableNewRow;

            gvSum.Init(dt);
            DataTable dtHareketTipi = AppSession.DataManager.ExecuteDataTable("GetHareketTipi");
            DataTable dtSirket = AppSession.DataManager.ExecuteDataTable("GetSirket");
            DataTable dtParabirimi = AppSession.DataManager.ExecuteDataTable("GetParabirimi");

            gvSum.Columns["HareketTipiId"].ColumnEdit = RepositoryManager.GetGLESablon(dtHareketTipi, "HareketTipi", "HareketTipiId");
            gvSum.Columns["SirketId"].ColumnEdit = RepositoryManager.GetGLESablon(dtSirket, "Sirket", "SirketId");
            gvSum.Columns["Parabirimi"].ColumnEdit = RepositoryManager.GetGLESablon(dtParabirimi, "Parabirimi", "Parabirimi");

            gvSum.AddSummary();

            gvSum.SetCaptions("ManualOdemeId:#", "SirketId:Şirket", "HareketTipiId:Hareket Tipi", "Aciklama:Açıklama");
            gvSum.MakeColumnInvisible("ReelKur", "ReelTL", "IslemTarihindekiKur", "IslemTarihindekiTL", "Kaydeden", "EklenmeTarihi");
            gvSum.FormatAsDate("Tarih");

            gvSum.FormatAsNumber(2, "Miktar");
            gvSum.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            gvSum.MakeEditable("ManualOdemeId");
            gvSum.BestFitColumns();

            gvSum.RowCellStyle += gvSum_RowCellStyle;

            gvSum.CellValueChanging += gvSum_CellValueChanging;


            this.Cursor = Cursors.Default;
        }

        void gvSum_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            if ("Tarih SirketId HareketTipiId Miktar".Contains(e.Column.FieldName) && e.CellValue.ToStringNN() == "")
            {
                e.Appearance.BackColor = Color.LightCoral;
            }
        }

        void dt_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["KaydiYapanId"] = AppSession.UserInfo.KullaniciId;
            e.Row["EklenmeTarihi"] = DateTime.Now;
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            gvSum.Update();
            gvSum.VeriyiGuncelle(true, "ManualGuncelle");
            gvSum.BestFitColumns();
        }
        public string Tatilmi(DateTime Tarih)
        {
            string Sonuc = "";
            DataTable dt = AppSession.DataManager.ExecuteDataSet("GetTatilmi", Tarih).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Sonuc = dt.Rows[0]["Aciklama"].ToString();
            }
            else if (Tarih.DayOfWeek == DayOfWeek.Saturday || Tarih.DayOfWeek == DayOfWeek.Sunday)
            {
                Sonuc = "haftasonu";
            }
            return Sonuc;
        }


        private void gvSum_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "Tarih")
            {
                DialogResult result = DialogResult.Yes;
                string TatilGunu = Tatilmi((DateTime)e.Value);
                if (TatilGunu != "")
                {
                    result = XtraMessageBox.Show("Seçtiğiniz gün,  " + TatilGunu + " .", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }


        }
    }
}